package cn.iocoder.yudao.module.wms.controller.admin.rdrecord07;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.inventory.stock.vo.InventoryStockReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord07.vo.Rdrecord07PageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord07.vo.Rdrecord07RespVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord07.vo.Rdrecord07SaveReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord07.vo.Rdrecords07RespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord07.Rdrecord07DO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord07.Rdrecords07DO;
import cn.iocoder.yudao.module.wms.service.inventory.stock.InventoryStockService;
import cn.iocoder.yudao.module.wms.service.rdrecord07.Rdrecord07Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 调拨出库主")
@RestController
@RequestMapping("/wms/rdrecord07")
@Validated
public class Rdrecord07Controller {

    @Resource
    private Rdrecord07Service rdrecord07Service;

    @Resource
    private InventoryStockService inventoryStockService;

    @PostMapping("/create")
    @Operation(summary = "创建调拨出库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:create')")
    public CommonResult<Long> createRdrecord07(@Valid @RequestBody Rdrecord07SaveReqVO createReqVO) {
        return success(rdrecord07Service.createRdrecord07(createReqVO));
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:production-order:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Long id) {
        rdrecord07Service.process(id);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新调拨出库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:update')")
    public CommonResult<Boolean> updateRdrecord07(@Valid @RequestBody Rdrecord07SaveReqVO updateReqVO) {
        rdrecord07Service.updateRdrecord07(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除调拨出库主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:delete')")
    public CommonResult<Boolean> deleteRdrecord07(@RequestParam("id") Long id) {
        rdrecord07Service.deleteRdrecord07(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得调拨出库主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:query')")
    public CommonResult<Rdrecord07RespVO> getRdrecord07(@RequestParam("id") String id) {
        Rdrecord07DO rdrecord07;
        try {
            rdrecord07 = rdrecord07Service.getRdrecord07(Long.valueOf(id));
        } catch (NumberFormatException e) {
            rdrecord07 = rdrecord07Service.getRdrecord07ByProcessInstanceId(id);
        }
        return success(BeanUtils.toBean(rdrecord07, Rdrecord07RespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得调拨出库主分页")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:query')")
    public CommonResult<PageResult<Rdrecord07RespVO>> getRdrecord07Page(@Valid Rdrecord07PageReqVO pageReqVO) {
        PageResult<Rdrecord07DO> pageResult = rdrecord07Service.getRdrecord07Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Rdrecord07RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出调拨出库主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRdrecord07Excel(@Valid Rdrecord07PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Rdrecord07DO> list = rdrecord07Service.getRdrecord07Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "调拨出库主.xls", "数据", Rdrecord07RespVO.class,
                        BeanUtils.toBean(list, Rdrecord07RespVO.class));
    }

    // ==================== 子表（调拨出库子） ====================

    @GetMapping("/rdrecords07/list-by-order-id")
    @Operation(summary = "获得调拨出库子列表")
    @Parameter(name = "orderId", description = "订单id")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord07:query')")
    public CommonResult<List<Rdrecords07RespVO>> getRdrecords07ListByOrderId(@RequestParam("orderId") Long orderId) {
        List<Rdrecords07DO> list = rdrecord07Service.getRdrecords07ListByOrderId(orderId);
        Rdrecord07DO rdrecord07 = rdrecord07Service.getRdrecord07(orderId);
        List<Rdrecords07RespVO> rdrecords07Resp = BeanUtils.toBean(list, Rdrecords07RespVO.class);
        rdrecords07Resp.forEach(rdrecords07RespVO -> {
            InventoryStockReqVO stockReqVO = new InventoryStockReqVO();
            stockReqVO.setInventoryId(rdrecords07RespVO.getInventoryId());
            stockReqVO.setWarehouseId(rdrecord07.getOutWarehouseId());
            stockReqVO.setLocationId(rdrecords07RespVO.getLocationId());
            stockReqVO.setBatchCode(rdrecords07RespVO.getBatch());
            rdrecords07RespVO.setAvailableAmount(inventoryStockService.getInventoryAvailableAmount(stockReqVO));
        });
        return success(rdrecords07Resp);
    }

}